#ifndef SNOWFLAKE_H
#define SNOWFLAKE_H

#include "Demo.h"
#include "RenderTarget.h"
#include "TextureCube.h"
#include "Random.h"
#include "VectorBatch.h"
#include "BitmapFont.h"
#include "Vertices.h"
#include "Mesh.h"

using namespace Cvekas;

class Snowflake : public TimelineSegment
{
public:
	Snowflake(RendererPtr renderer, VectorBatchPtr vector_batch, SpriteBatchPtr sprite_batch, BitmapFontPtr font);
	~Snowflake();

	virtual void init(boost::weak_ptr<Demo> owner);
	virtual void close();
	
	virtual void update(float t);
	virtual void render(float t);

private:
	Vector3 random_spherepoint();
	void genSnowflake(VertexPosNormal*& out, Vector3 p[3], uint level);
	void genCrystal(VertexPosNormal* out, uint branches);
	void genCrystalIB(uint16* out, uint branches);

	struct Hex 
	{
		Vector2 pos;
		Vector2 vel;
		float rot;
		float a_vel;
		float size;
		float t;
		Color c;
	};

	std::vector<Hex> hexes;

	void genHexes(uint count);
	void updateHexes(float t);
	void drawHexes(float t);

	RenderTargetPtr depth_target;
	RenderTargetPtr blur_target;

	RendererPtr renderer;
	VectorBatchPtr vector_batch;
	SpriteBatchPtr sprite_batch;
	BitmapFontPtr font;

	TextureCubePtr reflection;

	IndexBufferPtr crystal_ib;
	VertexBufferPtr crystal_vb;
	VertexBufferPtr snowflake_vb;
	
	RenderableMeshPtr snowflake;
	EffectPtr snowflake_effect;
	RenderableMeshPtr crystal;
	EffectPtr crystal_effect;
	EffectPtr hex_batch_effect;

	SimpleCamera camera;
	float light1_att, light2_att;

	Random prng;
};

typedef boost::shared_ptr<Snowflake> SnowflakePtr;

#endif